package cn.jiedh.app.meiweidk.login;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.MainActivity;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.event.Constant;
import cn.jiedh.app.meiweidk.Utils.event.EventScreen;
import cn.jiedh.app.meiweidk.Utils.event.EventUtils;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.Utils.tool.ValidateUtils;
import cn.jiedh.app.meiweidk.base.BaseFragment;
import cn.jiedh.app.meiweidk.buried.BuriedBean;
import cn.jiedh.app.meiweidk.entity.LoginAndRegisterBean;
import cn.jiedh.app.meiweidk.forgetpsw.ForgetPswActivity;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.register.RegisterActivity;
import cn.jiedh.app.meiweidk.response.BaseResponse;
import cn.jpush.android.api.JPushInterface;

/**
 * @name：密码登录
 * @author： 司马林
 * @phone： 18423134135
 * @createTime： 2017/10/19
 * @modifyTime： 2017/10/19
 */
public class PswLoginFragment extends BaseFragment {
    @BindView(R.id.phone)
    EditText phoneEdt;
    @BindView(R.id.psw_edt)
    EditText pswEdt;
    @BindView(R.id.coordinator)
    CoordinatorLayout coordinatorLayout;
    @BindView(R.id.login_btn)
    Button loginBtn;
    private List<BuriedBean> buriedList;

    private boolean isGoHome = false;
    @Override
    protected int getLayoutView() {
        return R.layout.fragment_psw_login;
    }

    @Override
    protected void initView(View view) {
        if (getActivity().getIntent().getExtras() != null) {
            isGoHome = getActivity().getIntent().getExtras().getBoolean("isGoHome");
        }
        pswEdt.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                String phone = phoneEdt.getText().toString();
                String psw = pswEdt.getText().toString();
                String errorStr = ValidateUtils.isLogin(phone, psw);
                if ("".equals(errorStr)) {
                    login(phone, psw);
                } else {
                    T.showShortCenterToast(errorStr);
                }
                return true;
            }
            return false;
        });
    }

    @OnClick({R.id.login_btn, R.id.forget_psw, R.id.fast_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_btn:
                MobclickAgent.onEvent(getActivity(), "password_login");  //umeng 登录
                MobclickAgent.onEvent(getActivity(), "login");  //umeng 登录
                InputMethodManager imm = (InputMethodManager) loginBtn.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(loginBtn.getWindowToken(), 0);

                String phone = phoneEdt.getText().toString();
                String psw = pswEdt.getText().toString();
                String errorStr = ValidateUtils.isLogin(phone, psw);
                if ("".equals(errorStr)) {
                    login(phone, psw);
                } else {
                    T.showShortCenterToast(errorStr);
                }
                break;
            case R.id.forget_psw:
                ActivityUtil.next(getActivity(), ForgetPswActivity.class);
                break;
            case R.id.fast_register:
                ActivityUtil.next(getActivity(), RegisterActivity.class);
                break;
            default:
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void login(String phone, String psw) {
        showLoadingDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("password", psw);
        showLoadingDialog();
        SPUtils.saveBuried("login","0","登录-密码登录","",
                "登录","","","");
        request(UrlUtils.PSWLOGIN, map, true, new ResponseSuccess<LoginAndRegisterBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<LoginAndRegisterBean> response) {
                dismissLoadingDialog();
                T.showShort(response.getMessage());
                if (0 == response.getCode()) {
                    SPUtils.saveUser(phone, response.getData().getToken());
                    JPushInterface.setAlias(getActivity(), SPUtils.getPhone(), null);
                    if (isGoHome) {
                        Bundle bundle = new Bundle();
                        bundle.putBoolean("isHome", true);
                        ActivityUtil.next(getActivity(), MainActivity.class, bundle);
                        EventScreen eventScreen=new EventScreen(Constant.NEWUSER,"");
                        EventUtils.getDefault().forward(eventScreen);
                    } else {
                        getActivity().finish();
                    }
                }
                L.i("token：" + response.getData().getToken());
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
    }
}
